import { defineConfig, loadEnv } from 'vite';
import uni from '@dcloudio/vite-plugin-uni';
// gzip压缩
import viteCompression from 'vite-plugin-compression';
import path from 'path';

// https://vitejs.dev/config/
// export default defineConfig({
//   plugins: [uni()],
// });

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, '');
  return {
    base: './',
    plugins: [
      uni(),
      viteCompression({
        // 记录压缩文件及其压缩率。默认true
        verbose: true,
        // 是否禁用压缩，默认false
        disable: false,
        // 需要使用压缩前的最小文件大小，单位字节（byte） b，1b(字节)=8bit(比特), 1KB=1024B
        threshold: 10240, // 即10kb以上即会压缩
        // 压缩算法 可选 'gzip' | 'brotliCompress' | 'deflate' | 'deflateRaw'
        algorithm: 'gzip',
        // 压缩后的文件格式
        ext: '.gz',
      }),
    ],

    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src'),
        '~': path.resolve(__dirname, 'src/components'),
      },
    },

    server: {
      host: true,
      port: 8080,
      // proxy,
    },

    build: {
      outDir: path.resolve(__dirname, 'dist'),
      minify: 'terser', // 设置terserOptions项才生效
      emptyOutDir: true,
      terserOptions: {
        compress: {
          // drop_console: mode !== "development",
          // drop_debugger: mode !== "development"
        },
      },
      sourcemap: mode === 'development',
      rollupOptions: {
        output: {
          chunkFileNames: 'assets/js/[name]-[hash].js',
          entryFileNames: 'assets/js/[name]-[hash].js',
          assetFileNames: 'assets/[ext]/[name]-[hash].[ext]',
          compact: true,
        },
      },
    },
  };
});
